﻿@{
    ViewBag.CurrentMenu = "Role";
    Layout = "_Layout";
}
<div class="layui-row">
        <div id="searchBar"></div>
        <table id="RoleTable" class="layui-table" lay-filter="RoleTable"></table>
</div>
<script type="text/javascript">
    var cols ="";
    layui.use(['table', 'jquery', 'searchTable'],
        function () {
            var table = layui.table, $ = layui.jquery, searchTable = layui.searchTable;
            //加载表头
            var loadCols = function () {
                $.ajax({
                    url: '@Url.Action("GetRoleTableCols", "Role")',
                    type: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.success) {
                            cols = [res.data];
                            loadTables();
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
            };
            //加载表格数据
            var loadTables = function () {
                var tableIns = table.render({
                    elem: '#RoleTable',
                    id: 'RoleTable',
                    url: "/Role/GetAllPageList?_t=" + new Date().getTime(),
                    page: true, //开启分页
                    loading: true,
                    method: 'post',
                    toolbar: '#toolbarButton',
                    height: 'full-120',
                    cols: cols
                });
                searchTable.init(tableIns);
            };
            $(document).ready(function () {
                loadCols();
            });
            //表头工具栏事件
            table.on('toolbar(RoleTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'authRole':
                        if (data.length < 1) {
                            top.layer.msg("请选择要授权的角色。", { icon: 0, offset: '15px' });
                            return false;
                        }
                        var nameStr = '';
                        var ids = '';
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].id + ',';
                            if (i === 0)
                                nameStr += data[i].name;
                            else
                                nameStr +=','+ data[i].name;
                        }
                        top.layer.open({
                            title: '为【' + nameStr+'】授权',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("authRole", "Role")?ids=' + ids,
                            end: function () {
                                table.reload("RoleTable");
                            }
                        });
                        break;
                    case 'editRole':
                        if (data.length !== 1) {
                            top.layer.msg("请选择一行数据。", { icon: 0, offset: '15px' });
                            return false;
                        }
                        top.layer.open({
                            title: '编辑',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "Role")?id=' + data[0].id,
                            end: function () {
                                table.reload("RoleTable");
                            }
                        });
                        break;
                    case 'addRole':
                        top.layer.open({
                            title: '新增',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "Role")',
                            end: function () {
                                table.reload("RoleTable");
                            }
                        });
                        break;
                    case 'delRole':
                        var ids = '';
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].id + ',';
                        }
                        if (ids.length < 1) {
                            top.layer.msg("请选择要删除的记录。", { icon: 0, offset: '15px' });
                            return;
                        }; //询问框
                        top.layer.confirm("您确认删除选定的记录吗？",
                            {
                                btn: ["确定", "取消"]
                            },
                            function () {
                                var sendData = { "ids": ids };
                                $.ajax({
                                    url: '@Url.Action("DeleteMuti", "Role")',
                                    type: 'post',
                                    data: sendData,
                                    dataType: 'json',
                                    success: function (res) {
                                        if (res.success) {
                                            top.layer.msg(res.msg, { icon: 1, offset: '15px' });
                                            table.reload("RoleTable");
                                        } else {
                                            top.layer.msg(res.msg, { icon: 2, offset: '15px' });
                                        }
                                    }
                                });
                            });
                        break;
                }
            });
        });
</script>